package com.yida.service;

import java.util.List;
import java.util.concurrent.TimeUnit;

import javax.mail.internet.MimeMessage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import com.yida.bean.blogroll;
import com.yida.dao.blogRollMapper;

@Service
public class bolgRollImpl implements blogRoll{

	@Autowired
	blogRollMapper mapper;
	
	@Autowired
	JavaMailSender MailSender;
	
	@Override
	public List<blogroll> blogRoolAll() {
		// TODO Auto-generated method stub
		return mapper.blogRoolAll();
	}
	

	@Override
	public boolean blogRoolAdd(blogroll blogroll) {
		// TODO Auto-generated method stub
		Integer sum = mapper.blogRoolAdd(blogroll) ;
		
		if (sum > 0) {
			
			try {
				
		    MimeMessage message = MailSender.createMimeMessage();
			MimeMessageHelper helper = new MimeMessageHelper(message, true);
			//谁发的
			helper.setFrom("1320123744@qq.com");
			 //谁要接收
			helper.setTo("1320123744@qq.com");
		    //邮件标题
			helper.setSubject("用户提交友链待验证");
		    //邮件内容
		    String html = "<p><span>网站名:"
			          +blogroll.getNickName()+
			          "</span><br> <span>描述:"
			          +blogroll.getMotto()+
			          "</span><br> <span>地址:"
			          +blogroll.getBlogHref()+
			          "</span><br> <span>头像:"
			          +blogroll.getImgUrl()+
			          "</p>";
		    
		    helper.setText(html,true);
		    MailSender.send(message);
	
		        return true;
		      } catch (Exception e) {
		        e.printStackTrace();
		        return false;
		      }
		    
		}else {
			return false;
		}
		
	}

}
